package com.tckdq.tcc.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tckdq.tcc.dto.CourseAddDto;
import com.tckdq.tcc.dto.CourseDetailDto;
import com.tckdq.tcc.service.ICourseService;
import com.tckdq.tcc.domain.Course;
import com.tckdq.tcc.query.CourseQuery;
import com.tckdq.tcc.result.JsonResult;
import com.tckdq.tcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.tckdq.tcc.vo.CourseDataForOrderVO;
import com.tckdq.tcc.vo.CourseStateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Component
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
    /*course/course/courseOrderDetail/ 课程订单详情*/
    @RequestMapping(value = "/courseOrderDetail/{courseStrIds}",method = RequestMethod.GET)
    public JsonResult selCourseForOrder(@PathVariable("courseStrIds")String courseStrIds){
        CourseDataForOrderVO orderVO = courseService.selCourseForOrder(courseStrIds);
        return JsonResult.success(orderVO);
    }

    //根据课程ID查询课程的状态： 是否免费，是否购买，是否上线
    @RequestMapping(value="/status/{id}",method= RequestMethod.GET)
    public JsonResult selectCourseStatusForUser(@PathVariable("id")Long id){
        CourseStateVO courseStatus = courseService.selectCourseStatusForUser(id);
        return JsonResult.success(courseStatus);
    }

    //获取课程详情页的的填充数据
    /*course/course/detail/data/"+this.courseId*/
    @RequestMapping(value = "/detail/data/{id}",method = RequestMethod.GET)
    public JsonResult getCourseDetailByCourseId(@PathVariable("id")Long id){
        CourseDetailDto courseDetailDtoList = courseService.getCourseDetailByCourseId(id);
        return JsonResult.success(courseDetailDtoList);
    }

    /*/course/course/gethotRecommendClourses  前台获取热门课程*/
    @Scheduled(fixedRate = 10000)//单位是毫秒
    @RequestMapping(value = "/gethotRecommendClourses",method = RequestMethod.GET)
    public JsonResult gethotRecommendClourses(){
        List<Course> hotRecommendCourses = courseService.gethotRecommendClourses();
        return JsonResult.success(hotRecommendCourses);
    }

    /*/course/course/getrecommendCourses/ 前台获取推荐课程*/
    @RequestMapping(value = "/getrecommendCourses",method = RequestMethod.GET)
    public JsonResult getrecommendCourses(){
        List<Course> recommendCourses = courseService.getrecommendCourses();
        return JsonResult.success(recommendCourses);
    }


    /*/course/course/recommendCourse/  课程推荐*/
    @RequestMapping(value="/recommendCourse/{id}",method= RequestMethod.POST)
    public JsonResult recommendCourse(@PathVariable("id")Long id){
        courseService.recommendCourse(id);
        return JsonResult.success();
    }


    /*/course/course/offLineCourse课程下架*/
    @RequestMapping(value="/offLineCourse/{id}",method= RequestMethod.POST)
    public JsonResult offLineCourse(@PathVariable("id")Long id){
        courseService.offLineCourse(id);
        return JsonResult.success();
    }
    /**
     * 课程上线
     */
    @RequestMapping(value="/onLineCourse/{id}",method= RequestMethod.POST)
    public JsonResult onLineCourse(@PathVariable("id")Long id){
        courseService.onLineCourse(id);
        return JsonResult.success();
    }

    /*/course/course/listForUser*/
    @RequestMapping(value = "/listForUser",method = RequestMethod.GET)
    public JsonResult listForUser(){
        return JsonResult.success(courseService.listForUser());
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody @Valid CourseAddDto ourseAddDto){

            courseService.save(ourseAddDto);

        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JsonResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
